package game.platformer.levels;

import game.platformer.Player;
import game.platformer.interfaces.MapScreen;
import game.platformer.platformer;
import java.util.ArrayList;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;

/**
 *
 * @author sOaF
 */
public final class levelThree implements Level {
    //varblock-------------------------

    private platformer game;
    private boolean loaded;  //is the level on screen?
    private ArrayList<levelExit> exits;
    private int height, width;
    private levelDeathBox boundry;
    levelExit forwardExit;
    levelExit backwardExit;
    //platforms------------------------
    levelPlatform[] platforms;
    //---------------------------------
    //constructor================================

    public levelThree(platformer game) {
        //save paramiters
        this.game = game;

        //initialize lists
        exits = new ArrayList<levelExit>();

        //initialize data
        height = 1000;
        width = 2000;
        loaded = false;
    }
    //load up the textures=======================

    public void loadTextures() {
    }
    //level loading textures=====================

    private void loadExits() {
        forwardExit = new levelExit(850, 50, 100, 100, 3);//exit location, next level entrance location, levelID
        backwardExit = new levelExit(850, 550, 100, 100, 1);
    }



    public void loadLevel(int entranceX, int entreanceY) {
        //references-------------------
        iceTextureSet texset = platformer.getLevelManager().getIceMainPlatform();
        MapScreen scr = platformer.getScreenManager().getMapScreen();

        //set background---------------
        Sprite temp = new Sprite(0, 0, texset.getBackground());
        scr.setBackground(new SpriteBackground(temp));

        //place the deathBox-----------
        boundry = new levelDeathBox(width, height);
        boundry.setLastEntryPoint(entranceX, entreanceY);
        boundry.place();

        //setup platforms--------------
        platforms = new levelPlatform[]{
            new levelPlatform(texset.getIceMainPlatform(), 100, 600),
            new levelPlatform(texset.getIceMainPlatform(), 300, 400),
            new levelPlatform(texset.getIceMainPlatform(), 500, 300),
            new levelPlatform(texset.getIceMainPlatform(), 800, 100),
            new levelPlatform(texset.getIceMainPlatform(), 800, 600)
        };
        //place platforms--------------
        for (levelPlatform p : platforms) {
            p.place();
        }
        
        //load and place exit
         loadExits();
        forwardExit.place();
        backwardExit.place();

        //place the player-------------
        Player.getPlayer().place(entranceX, entreanceY);


    }
        public boolean isLoaded() {
        return loaded;
    }

    public void unloadLevel() {
        //references-------------------
        final MapScreen scr = platformer.getScreenManager().getMapScreen();
        final PhysicsWorld pw = platformer.getPhysicsWorld();
        final IUpdateHandler removalHandler;
        //-----------------------------

        //this function is called by map when updateing a frame
        //we do this so that items arnt removed between updates
        removalHandler = new IUpdateHandler() {

            public void onUpdate(float pSecondsElapsed) {

                //pull the platforms from the mapscreen
                for (levelPlatform p : platforms) {
                    scr.detachChild(p.getSpriteRep());
                    pw.unregisterPhysicsConnector(p.getConnector());
                    pw.destroyBody(p.getBodyRep());
                }

                //kill the death box
                scr.detachChild(boundry);

               //kill exits
                scr.detachChild(forwardExit);
                scr.detachChild(backwardExit);
                //remove the handler
                scr.unregisterUpdateHandler(this);
            }

            public void reset() {
            }
        };

        //start the removal
        scr.registerUpdateHandler(removalHandler);
        loaded = false;
    }
    //accessors==================================

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getNumExits() {
        return 0;
    }
    //returns if the param collides with the platforms
//bullet stuff

    public boolean colidesWithPlatform(Sprite colideable) {
        for (levelPlatform p : platforms) {
            if (p.getSpriteRep().collidesWith(colideable)) {
                return true;
            }
        }
        return false;
    }
}
